<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>axios学习 | 诶无痕和诶有痕</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="axios安装1npm install axios 特性123456781.从浏览器中创建XMLHttpRequests2.从node.js创建http请求3.支持PromiseAPI4.拦截请求和响应5.转换请求数据和响应数据6.取消请求7.自动转换JSOn数据8.客户端支持防御SXRF 使用get123456789101112131415161718192021&#x2F;&#x2F; 为给定 ID 的 user">
<meta property="og:type" content="article">
<meta property="og:title" content="axios学习">
<meta property="og:url" content="http://example.com/2022/03/20/axios%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="诶无痕和诶有痕">
<meta property="og:description" content="axios安装1npm install axios 特性123456781.从浏览器中创建XMLHttpRequests2.从node.js创建http请求3.支持PromiseAPI4.拦截请求和响应5.转换请求数据和响应数据6.取消请求7.自动转换JSOn数据8.客户端支持防御SXRF 使用get123456789101112131415161718192021&#x2F;&#x2F; 为给定 ID 的 user">
<meta property="og:locale" content="zh_CH">
<meta property="article:published_time" content="2022-03-20T12:03:05.000Z">
<meta property="article:modified_time" content="2022-03-20T12:08:12.937Z">
<meta property="article:author" content="诶无痕和诶有痕">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="诶无痕和诶有痕" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">诶无痕和诶有痕</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-axios学习" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/03/20/axios%E5%AD%A6%E4%B9%A0/" class="article-date">
  <time datetime="2022-03-20T12:03:05.000Z" itemprop="datePublished">2022-03-20</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      axios学习
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="axios"><a href="#axios" class="headerlink" title="axios"></a>axios</h1><h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install axios</span><br></pre></td></tr></table></figure>
<h2 id="特性"><a href="#特性" class="headerlink" title="特性"></a>特性</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1.从浏览器中创建XMLHttpRequests</span><br><span class="line">2.从node.js创建http请求</span><br><span class="line">3.支持PromiseAPI</span><br><span class="line">4.拦截请求和响应</span><br><span class="line">5.转换请求数据和响应数据</span><br><span class="line">6.取消请求</span><br><span class="line">7.自动转换JSOn数据</span><br><span class="line">8.客户端支持防御SXRF</span><br></pre></td></tr></table></figure>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><h3 id="get"><a href="#get" class="headerlink" title="get"></a>get</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">// 为给定 ID 的 user 创建请求</span><br><span class="line">axios.get(&#x27;/user?ID=12345&#x27;)</span><br><span class="line">  .then(function (response) &#123;</span><br><span class="line">    console.log(response);</span><br><span class="line">  &#125;)</span><br><span class="line">  .catch(function (error) &#123;</span><br><span class="line">    console.log(error);</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">// 上面的请求也可以这样做</span><br><span class="line">axios.get(&#x27;/user&#x27;, &#123;</span><br><span class="line">    params: &#123;</span><br><span class="line">      ID: 12345</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  .then(function (response) &#123;</span><br><span class="line">    console.log(response);</span><br><span class="line">  &#125;)</span><br><span class="line">  .catch(function (error) &#123;</span><br><span class="line">    console.log(error);</span><br><span class="line">  &#125;);</span><br></pre></td></tr></table></figure>
<h3 id="post"><a href="#post" class="headerlink" title="post"></a>post</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">axios.post(&#x27;/user&#x27;, &#123;</span><br><span class="line">    firstName: &#x27;Fred&#x27;,</span><br><span class="line">    lastName: &#x27;Flintstone&#x27;</span><br><span class="line">  &#125;)</span><br><span class="line">  .then(function (response) &#123;</span><br><span class="line">    console.log(response);</span><br><span class="line">  &#125;)</span><br><span class="line">  .catch(function (error) &#123;</span><br><span class="line">    console.log(error);</span><br><span class="line">  &#125;);</span><br></pre></td></tr></table></figure>
<h4 id="执行多个并发请求"><a href="#执行多个并发请求" class="headerlink" title="执行多个并发请求"></a>执行多个并发请求</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">function getUserAccount() &#123;</span><br><span class="line">  return axios.get(&#x27;/user/12345&#x27;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function getUserPermissions() &#123;</span><br><span class="line">  return axios.get(&#x27;/user/12345/permissions&#x27;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">axios.all([getUserAccount(), getUserPermissions()])</span><br><span class="line">//第一个参数，是第一个接口的值</span><br><span class="line">  .then(axios.spread(function (acct, perms) &#123;</span><br><span class="line">    // 两个请求现在都执行完成</span><br><span class="line">  &#125;));</span><br></pre></td></tr></table></figure>
<h3 id="axios-API"><a href="#axios-API" class="headerlink" title="axios API"></a>axios API</h3><p>可以通过向 axios 传递相关配置来创建请求</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">// 发送 POST 请求</span><br><span class="line">axios(&#123;</span><br><span class="line">  method: &#x27;post&#x27;,</span><br><span class="line">  url: &#x27;/user/12345&#x27;,</span><br><span class="line">  //post类型使用data，get类型使用params</span><br><span class="line">  data: &#123;</span><br><span class="line">    firstName: &#x27;Fred&#x27;,</span><br><span class="line">    lastName: &#x27;Flintstone&#x27;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="创建axios实例"><a href="#创建axios实例" class="headerlink" title="创建axios实例"></a>创建axios实例</h2><h3 id="axios-create-config"><a href="#axios-create-config" class="headerlink" title="axios.create([config])"></a>axios.create([config])</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const instance = axios.create(&#123;</span><br><span class="line">  baseURL: &#x27;https://some-domain.com/api/&#x27;,</span><br><span class="line">  timeout: 1000,</span><br><span class="line">  headers: &#123;&#x27;X-Custom-Header&#x27;: &#x27;foobar&#x27;&#125;</span><br><span class="line">&#125;); </span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">import axios from &quot;axios&quot;;</span><br><span class="line">import qs from &quot;qs&quot;;</span><br><span class="line">const hxios = axios.create(&#123;</span><br><span class="line">  baseURL:&quot;/api&quot;,</span><br><span class="line">  transformRequest:[</span><br><span class="line">    function(data,headers) &#123;</span><br><span class="line">      console.log(&quot;----------------------&quot;);</span><br><span class="line">      // qs是用来对数据进行序列化的</span><br><span class="line">      return qs.stringify(data);</span><br><span class="line">    &#125;,</span><br><span class="line">  ],</span><br><span class="line">  timeot:4000,</span><br><span class="line">&#125;);</span><br><span class="line">export default hxios;</span><br></pre></td></tr></table></figure>
<h2 id="二次封装axios"><a href="#二次封装axios" class="headerlink" title="二次封装axios"></a>二次封装axios</h2><p>创建一个axios.js文件<br>在文件中二次封axios<br>将二次封装的axios放到vue原型上（或者直接在要使用的文件中引用）<br>例如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">const axios = require(&#x27;axios&#x27;);</span><br><span class="line"></span><br><span class="line">const request = axios.create(&#123;</span><br><span class="line">  // 自动让添加到请求路径上，被代理服务器拦截后，可以通过代理服务器将baseYURL去掉</span><br><span class="line">  baseURL:&quot;/api&quot;,</span><br><span class="line">  // 设置请求超时时间，一般设置为3，4秒</span><br><span class="line">  timeout:4000,</span><br><span class="line">  // `params` 是即将与请求一起发送的 URL 参数</span><br><span class="line">  // 必须是一个无格式对象(plain object)或 URLSearchParams 对象</span><br><span class="line">  // get请求时使用</span><br><span class="line">  params: &#123;</span><br><span class="line">    ID: 12345</span><br><span class="line">  &#125;,</span><br><span class="line">  // post请求时使用的传输数据的对象</span><br><span class="line">  // data: &#123;</span><br><span class="line">  //   firstName: &#x27;Fred&#x27;</span><br><span class="line">  // &#125;,</span><br><span class="line">  // header是即将被发送的自定义请求头</span><br><span class="line">  // headers:&#123;&#x27;X-Requested-With&#x27;: &#x27;XMLHttpRequest&#x27;&#125;,</span><br><span class="line">  // `method` 是创建请求时使用的方法</span><br><span class="line">  // method: &#x27;get&#x27;, // default</span><br><span class="line">  // `transformRequest` 允许在向服务器发送前，修改请求数据</span><br><span class="line">  // 只能用在 &#x27;PUT&#x27;, &#x27;POST&#x27; 和 &#x27;PATCH&#x27; 这几个请求方法</span><br><span class="line">  // 后面数组中的函数必须返回一个字符串，或 ArrayBuffer，或 Stream</span><br><span class="line">  // transformRequest: [function (data, headers) &#123;</span><br><span class="line">  //   // 对 data 进行任意转换处理</span><br><span class="line">  //   return data;</span><br><span class="line">  // &#125;],</span><br><span class="line"></span><br><span class="line">  // `transformResponse` 在传递给 then/catch 前，允许修改响应数据</span><br><span class="line">  transformResponse: [function (data) &#123;</span><br><span class="line">    // 对 data 进行任意转换处理</span><br><span class="line">    return data;</span><br><span class="line">  &#125;],</span><br><span class="line"></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">module.exports = request;</span><br></pre></td></tr></table></figure>
<h1 id="跨域问题的解决"><a href="#跨域问题的解决" class="headerlink" title="跨域问题的解决"></a>跨域问题的解决</h1><h2 id="方法一"><a href="#方法一" class="headerlink" title="方法一"></a>方法一</h2><p>在服务端设置白名单</p>
<h2 id="方法二"><a href="#方法二" class="headerlink" title="方法二"></a>方法二</h2><p>设置一个本地代理服务器，用代理服务器请求接口服务器，在通过代理服务器将数据发送给浏览器（页面）</p>
<h2 id="设置全局axios"><a href="#设置全局axios" class="headerlink" title="设置全局axios"></a>设置全局axios</h2><p>在main.js中引入axios，并将axios放到vue的原型中，例如：<br>Vue.prototype.$axios=axios</p>
<h3 id="例子："><a href="#例子：" class="headerlink" title="例子："></a>例子：</h3><p>过程：客户端发起请求，被代理服务器拦截，然后代理服务器将数据放松给目标服务器</p>
<p>main.js文件中添加以下代码,将axios放到vue的原型中</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import axios from &#x27;axios&#x27;</span><br><span class="line">Vue.prototype.$axios = axios;</span><br></pre></td></tr></table></figure>
<p>vue.config.js文件中写入以下代理服务器拦截请求的代码（vue.config.js文件没有就自己创建）】<br>在更目录下创建vue.config.js配置文件<br>关于打包的信息都可以使用vue.config.js配置</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">//过程：客户端发起请求，被代理服务器拦截，然后代理服务器将数据放松给目标服务器</span><br><span class="line">module.exports =&#123;</span><br><span class="line">  // 用来设置本地开发服务的</span><br><span class="line">  devServer: &#123;</span><br><span class="line">    //代理</span><br><span class="line">    proxy: &#123;</span><br><span class="line">      //拦截一个请求</span><br><span class="line">      // 如果请求接口有/api，就将这个请求代理到target的url这个地址中</span><br><span class="line">      &quot;/api&quot; : &#123;</span><br><span class="line">        target: &quot;http://192.168.0.13:8081&quot;,</span><br><span class="line">        //是否返回数据</span><br><span class="line">        changeOrigin:true,</span><br><span class="line">        //路径重写</span><br><span class="line">        pathRewrite:&#123;</span><br><span class="line">          //通过正则</span><br><span class="line">          //将以/api开头的路径中的/api替换成空字符串</span><br><span class="line">          &quot;^/api&quot;:&quot;&quot;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<p>创建server.js文件，用以写接口代码（或不要这个文件，直接去请求接口）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">const Koa  = require(&#x27;koa&#x27;);</span><br><span class="line">const Router = require(&#x27;koa-router&#x27;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">const app = new Koa();</span><br><span class="line">const router = new Router();</span><br><span class="line"></span><br><span class="line">router.get(&quot;/api&quot;,(ctx) =&gt;&#123;</span><br><span class="line">  ctx.body = &#x27;你φ(*￣0￣)&#x27;;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.use(router.routes()).use(router.allowedMethods());</span><br><span class="line"></span><br><span class="line">app.listen(8081,()=&gt;&#123;</span><br><span class="line">  console.log(&quot;server is running &quot;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>调用axios请求接口</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">changeMsg:function() &#123;</span><br><span class="line">      this.$axios.get(&#x27;/api&#x27;,&#123;</span><br><span class="line">        params:&#123;</span><br><span class="line">          Id:10</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;).then(res =&gt; &#123;</span><br><span class="line">        console.log(res);</span><br><span class="line">      &#125;).catch(error =&gt; &#123;</span><br><span class="line">        console.log(error);</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>过程</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">1引入axios</span><br><span class="line">2.发送请求</span><br><span class="line">3.解决跨域，服务器代理</span><br><span class="line">  1：新建vue.config.js（项目更目录）</span><br><span class="line">  2：写代理配置</span><br><span class="line">  module.exports =&#123;</span><br><span class="line">  // 用来设置本地开发服务的</span><br><span class="line">  devServer: &#123;</span><br><span class="line">    proxy: &#123;</span><br><span class="line">      // 如果请求接口有/api，就将这个请求代理到target的&lt;url&gt;这个地址中</span><br><span class="line">      &quot;/api&quot; : &#123;</span><br><span class="line">        trget: &quot;http://192.168.0.13:8080/&quot;,</span><br><span class="line">        changeOrigin:true,</span><br><span class="line">        //路径重写</span><br><span class="line">        pathRewrite:&#123;</span><br><span class="line">          //通过正则</span><br><span class="line">          //将以/api开头的路径中的/api替换成空字符串</span><br><span class="line">          &quot;^/api&quot;:&quot;&quot;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="拦截器"><a href="#拦截器" class="headerlink" title="拦截器"></a>拦截器</h2><p>拦截器的用法和生命周期有点像</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">const axios = require(&#x27;axios&#x27;);</span><br><span class="line"></span><br><span class="line">const req = axios.create(&#123;</span><br><span class="line">  // 自动让添加到请求路径上，被代理服务器拦截后，可以通过代理服务器将baseYURL去掉</span><br><span class="line">  baseURL:&quot;/api&quot;,</span><br><span class="line">  // 设置请求超时时间，一般设置为3，4秒</span><br><span class="line">  timeout:4000,</span><br><span class="line">  // `params` 是即将与请求一起发送的 URL 参数</span><br><span class="line">  // 必须是一个无格式对象(plain object)或 URLSearchParams 对象</span><br><span class="line">  // get请求时使用</span><br><span class="line">  params: &#123;</span><br><span class="line">    ID: 12345</span><br><span class="line">  &#125;,</span><br><span class="line">  // post请求时使用的传输数据的对象</span><br><span class="line">  // data: &#123;</span><br><span class="line">  //   firstName: &#x27;Fred&#x27;</span><br><span class="line">  // &#125;,</span><br><span class="line">  // header是即将被发送的自定义请求头</span><br><span class="line">  // headers:&#123;&#x27;X-Requested-With&#x27;: &#x27;XMLHttpRequest&#x27;&#125;,</span><br><span class="line">  // `method` 是创建请求时使用的方法</span><br><span class="line">  // method: &#x27;get&#x27;, // default</span><br><span class="line">  // `transformRequest` 允许在向服务器发送前，修改请求数据</span><br><span class="line">  // 只能用在 &#x27;PUT&#x27;, &#x27;POST&#x27; 和 &#x27;PATCH&#x27; 这几个请求方法</span><br><span class="line">  // 后面数组中的函数必须返回一个字符串，或 ArrayBuffer，或 Stream</span><br><span class="line">  // transformRequest: [function (data, headers) &#123;</span><br><span class="line">  //   // 对 data 进行任意转换处理</span><br><span class="line">  //   return data;</span><br><span class="line">  // &#125;],</span><br><span class="line"></span><br><span class="line">  // `transformResponse` 在传递给 then/catch 前，允许修改响应数据</span><br><span class="line">  transformResponse: [function (data) &#123;</span><br><span class="line">    // 对 data 进行任意转换处理</span><br><span class="line">    return data;</span><br><span class="line">  &#125;],</span><br><span class="line"></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">// 请求拦截器</span><br><span class="line">req.interceptors.request.use(</span><br><span class="line">  function (config) &#123;</span><br><span class="line">    // 在发送请求之前做些什么</span><br><span class="line">    return config;</span><br><span class="line">  &#125;,</span><br><span class="line">  function (error) &#123;</span><br><span class="line">    // 对请求错误做些什么 包含了状态码</span><br><span class="line">    return Promise.reject(error);</span><br><span class="line">  &#125;</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 响应拦截器</span><br><span class="line">req.interceptors.response.use(</span><br><span class="line">  function (response) &#123;</span><br><span class="line">    // 对响应数据做点什么</span><br><span class="line">    return response;</span><br><span class="line">  &#125;,</span><br><span class="line">  function (error) &#123;</span><br><span class="line">    // 对响应错误做点什么</span><br><span class="line">    console.log(error, &quot;-------------&quot;);</span><br><span class="line">    return Promise.reject(&quot;你出错了！但是呢就不告诉哪里错了&quot;);</span><br><span class="line">  &#125;</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">module.exports = req;</span><br></pre></td></tr></table></figure>
<h1 id="数据格式-在浏览器中"><a href="#数据格式-在浏览器中" class="headerlink" title="数据格式(在浏览器中)"></a>数据格式(在浏览器中)</h1><p>get请求提交的数据格式：query String<br>post请求提交的数据： Payload<br>form表单提交的数据：form data</p>
<h1 id="常见状态码"><a href="#常见状态码" class="headerlink" title="常见状态码"></a>常见状态码</h1><p>401,403,404,500,501,502<br>400：Bad Request–客户端请求的语法错误，服务器无法理解<br>401:没有权限（例如：没有带校验信息）<br>403：forbidden–服务器理解请求客户端的请求，但是拒绝执行此请求<br>404;NOT Found–服务器无法根据客户端的请求找到资源（网页）。（与401类似）‘<br>500：Internal Server Error – 服务器内部错误，无法完成请求<br>501：Not Implemented–服务器不支持请求的功能，无法完成请求<br>502：Bad Gateway–作为网关或者代理工作的服务器尝试执行请求时，从远程服务器接收到了一个无效的响应<br>503：Service Unavailable–由于超载或系统维护，服务器暂时的无法处理客户端的请求。延时的长度可包含在服务器的Retry-After头信息中    </p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/03/20/axios%E5%AD%A6%E4%B9%A0/" data-id="cl0z8jhwu000034w60af4dx9x" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/03/22/vue-router%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          vue-router的创建过程
        
      </div>
    </a>
  
  
    <a href="/2022/03/16/%E4%BA%86%E8%A7%A3vue%E7%BB%84%E4%BB%B6/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">了解vue组件</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2022/03/23/vuex%E5%AD%A6%E4%B9%A0/">vuex学习</a>
          </li>
        
          <li>
            <a href="/2022/03/22/vuew-router%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/">vuew-router的基本使用</a>
          </li>
        
          <li>
            <a href="/2022/03/22/vue-router%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/">vue-router的创建过程</a>
          </li>
        
          <li>
            <a href="/2022/03/20/axios%E5%AD%A6%E4%B9%A0/">axios学习</a>
          </li>
        
          <li>
            <a href="/2022/03/16/%E4%BA%86%E8%A7%A3vue%E7%BB%84%E4%BB%B6/">了解vue组件</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2022 诶无痕和诶有痕<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>